import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welco from '../components/Welcome.vue'
import Users from '../components/user/users.vue'
import Rights from '../components/power/rights.vue'
import Rolse from '../components/power/Rolse.vue'
import CAte from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import GoodsList from '../components/goods/list.vue'
import Add from '../components/goods/add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'

// 测试
import Ceshi from '../components/Ceshi.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', component: Login },
  {
    path: '/home',
    component: Home,
    children: [
      { path: '/home', component: Welco },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Rolse },
      { path: '/categories', component: CAte },
      { path: '/ceshi', component: Ceshi },
      { path: '/params', component: Params },
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  },
  {
    path: '*',
    name: '错误',
    component: () => import('../components/Error.vue')
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/') next()
  const tokenStr = window.sessionStorage.getItem('token')
  // 增加判断条件
  if (!tokenStr && to.path !== '/') next('/')
  next()
})
export default router
